<template>
  <div>
    <div v-for="(menu, index) in filteredList" :key="index">
      <div class="menu-title-container">
        <template v-if="menu.icon">
          <img :src="menu.icon" alt="" class="menu-icon">
        </template>
        <span class="menu-title">{{menu.title}}</span>
      </div>
      <ul class="menu-items-container">
        <li v-for="(item, iIndex) in menu.items"
            :key="iIndex"
            @click="onClick($event, index, iIndex)"
            :class="{'menu-item':true,'menu-item-selected':active[index][iIndex]}">
          <router-link :to="`/${menuList.baseRoute}/${item.route}`">{{item.text}}</router-link>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  name: "MenuList",
  props: {
    menuList: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      active: [],
      filteredList: this.initFilteredList(),
    }
  },
  methods: {
    onClick(event, pIndex,index) {
      // this.active = this.active.map((item1, index1) => item1.map((item2, index2) => index1 == pIndex && index2 == index));
      this.$emit("onClick",event, pIndex, index);
    },
    initActiveList() {
      if(this.menuList) {
        this.active = new Array(this.menuList.menuList.length);
        this.menuList.menuList.forEach((item, index) => {
          this.active[index] = new Array(item.items.length).fill(false)
          item.items.forEach((item, index1) => item.default && (this.active[index][index1] = true))
        })
      }
    },
    updateActiveList() {
      if(this.menuList) {
        let paths = this.$route.fullPath.split("/");paths.shift();
        let index = paths.indexOf(this.menuList.baseRoute) + 1;
        if(typeof (paths[index]) === "undefined") paths[index] = ""; //处理当前页面的根路径
        this.active = this.menuList.menuList.map((item1) => item1.items.map((item2) => paths[index] == item2.route));
  
      }
    }
  },
  watch: {
    menuList() {
      this.initActiveList();
      this.updateActiveList();
    },
    $route() {
      this.updateActiveList();
    }
  },
  created() {
    this.initActiveList();
    this.updateActiveList();
  },
  beforeCreate() {
    this.user = JSON.parse(localStorage.getItem('user'));

    this.initFilteredList = function () {
      if(!this.user || !this.menuList) return [];
      let index = `/${this.menuList.baseRoute}`.length + 1;
      let routes = this.user.userRoles
          .filter( item => item.indexOf(this.menuList.baseRoute) != -1)
          .map(item => item.substring(index));
      let menu = [];
      this.menuList.menuList.forEach(l1 => {
        if(l1.items.some(l2 => routes.includes(l2.route))) {
          let l1c = this._.cloneDeep(l1);
          l1c.items = l1.items.filter(a => routes.includes(a.route));
          menu.push(l1c);
        }
      })
      console.log("permission")
      console.log(this.user)
      console.log(menu)
      return menu;
    }
  }
}
</script>

<style scoped>
  .menu-title-container {
    padding: 10px 0;
  }

  .menu-icon {
    width: 20px;
    margin: 0 10px 0 15px;
  }
  
  .menu-title {
    font-weight: 600;
  }
  
  .menu-item {
    border-bottom: 1px solid #c8c8c8;
    background-color: transparent;
    transition: background-color 300ms;
  }

  .menu-item:hover {
    background-color: rgba(144, 144, 144, 0.2);
  }
  
  .menu-item-selected {
    background-color: rgba(144, 144, 144, 0.2);
    pointer-events: none;
  }
  
  .menu-item a {
    display: block;
    text-decoration: none;
    color: #555;
    padding: 10px 0 10px 60px;
    background-color: transparent;
  }
  
</style>